/**
 * Copyright(c) Yystar Technology Co.,Ltd
 * project：converter
 * package：com.yystar.common.util.date
 * fileName：DateUtil.java
 * date：2012-9-11
 */
package com.yystar.security.support.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

/**
 * @author wangshiyong
 * @date 2012-9-11
 * @todo 日期时间工具类
 * @modify
 * 1.2012-9-11 wangshiyong Create file.
 */
public class DateUtil {

	public static final String DATE_TIME_ALL_PARSE_PATTERN = "yyyy-MM-dd HH:mm:ss:SSS";
	public static final String DATE_TIME_DEFAULT_PARSE_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_EVENT_PARSE_PATTERN = "yyyy-MM-dd HH:mm";
	public static final String DATE_DEFAULT_PARSE_PATTERN = "yyyy-MM-dd";
	public static final String COOKIE_TIMEZONE_KEY = "timezone";
	public static final String DEFAULT_TIME_ZONE = "GMT-4:00";
	public static String CURRENT_TIME_ZONE;
	
	public static Date initDate = null;
	static {
		initDate = parse("1900-01-01", DATE_DEFAULT_PARSE_PATTERN);
	}

	public static Date parse(String text) {
		return parse(text, "");
	}
	
	/**
	 * setter time zone
	 * @param timeZone
	 */
	public static void setTimeZone(String timeZone) {
		if (!timeZone.equals(""))
			CURRENT_TIME_ZONE = timeZone;
		else
			CURRENT_TIME_ZONE = DEFAULT_TIME_ZONE;
	}
    /**
     *  DateTime Format
     */
	public static SimpleDateFormat getDateFormatter(String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);

		sdf.setTimeZone(TimeZone.getTimeZone(DEFAULT_TIME_ZONE));

		return sdf;
	}
	
	/**
	 * Date Format
	 * @param sdf
	 * @param date
	 * @return
	 */
	public static String formatDate(SimpleDateFormat sdf, java.util.Date date) {
		String result = "";

		try {
			if (date != null) {
				result = sdf.format(date);
			}
		} catch (Exception ex) {

		}

		return result;
	}
	/**
	 * Get Date
	 * 
	 * @return
	 */
	public static Date getTodayDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date result = new Date();

		try {
			result = sdf.parse(sdf.format(result) + " 00:00:00");
		} catch (ParseException ex) {
			result = new Date();
		}

		return result;
	}

	public static Date parse(String text, String format) {
		java.util.Date result = null;

		if (StringUtils.isEmpty(format)) {
			format = DATE_TIME_DEFAULT_PARSE_PATTERN;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		try {
			result = sdf.parse(text);
		} catch (ParseException ex) {
			result = new java.util.Date();
		}

		return result;
	}

	public static String formatDate(Date date) {
		String result;

		SimpleDateFormat sdf = new SimpleDateFormat(DATE_DEFAULT_PARSE_PATTERN);
		try {
			result = sdf.format(date);
		} catch (Exception ex) {
			result = "";
		}

		return result;
	}
	
	/**
	 * Format date to special format string
	 * @param date Date
	 * @param parseFormat String  datetime format
	 * @return string
	 */
	public static String formatDate(Date date, String parseFormat) {
		String result;

		if (StringUtils.isEmpty(parseFormat)) {
			parseFormat = DATE_TIME_DEFAULT_PARSE_PATTERN;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(parseFormat);
		try {
			result = sdf.format(date);
		} catch (Exception ex) {
			result = "";
		}

		return result;
	}

	public static boolean checkDateFormat(Date date) {
		String strDate = StringUtil.formatDate(StringUtil.getDateFormatter(DATE_TIME_DEFAULT_PARSE_PATTERN), date);
		String eL = "^(\\d{4})-(\\d{1,2})-(\\d{1,2}) (0\\d{1}|1\\d{1}|2[0-3]):[0-5]\\d{1}:([0-5]\\d{1})$";
		Pattern p = Pattern.compile(eL);
		Matcher m = p.matcher(strDate);
		return m.matches();
	}
	
	/**
	 * Add Second
	 * 
	 * @param date
	 * @param secondDiff
	 * @return
	 */
	public static Date dateAddSecond(java.util.Date date, int secondDiff) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		
		cal.add(Calendar.SECOND, secondDiff);
		
		return cal.getTime();
	}

	/**
	 * Add Day
	 * 
	 * @param date
	 * @param dayDiff
	 * @return
	 */
	public static Date dateAddDay(java.util.Date date, int dayDiff) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);

		cal.add(Calendar.DAY_OF_MONTH, dayDiff);

		return cal.getTime();
	}


	public static String formatDate(String timeZone, String pattern, Date date) {
		if (StringUtils.isEmpty(pattern)) {
			pattern = DATE_TIME_DEFAULT_PARSE_PATTERN;
		}
		if (StringUtils.isEmpty(timeZone)) {
			timeZone = DEFAULT_TIME_ZONE;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		
		sdf.setTimeZone(TimeZone.getTimeZone(timeZone));
		
		return sdf.format(date);
	}	
	
	public static int getOffset(String timeZoneId) {
		return TimeZone.getTimeZone(timeZoneId).getRawOffset() - TimeZone.getTimeZone(DEFAULT_TIME_ZONE).getRawOffset();
	}
	
	public static Date getDefaultTimeZoneDate(String timeZoneId, java.util.Date date) {
		int offSet = DateUtil.getOffset(timeZoneId);
		java.util.Date returDate = new java.util.Date(date.getTime() + offSet);
		return returDate;
	}
	
	public static void main(String[] args) {
		String currentDate = DateUtil.formatDate(new Date(), DateUtil.DATE_TIME_ALL_PARSE_PATTERN);
		System.out.println("currentDateTime:" + currentDate);
		System.out.println("currentDate:" + currentDate.substring(0, 10));
		System.out.println("currentTime:" + currentDate.substring(11, 16));
		System.out.println("currentTime:" + DateUtil.dateAddSecond(new Date(), -120).before(new Date()));
	}
}
